'use strict';

/* Controllers */



function userCtrl($scope, $stateParams, $http, $location) {

  $http.get('user/' + $stateParams.id).success(function(data) {
    $scope.user = data;
 
    $('#datetimepicker').datetimepicker({
        format: 'dd/MM/yyyy hh:mm:ss'
      });
  });
  
  $scope.over = function(user){
	  alert(user.name);
  };
  $scope.leave = function(user){
	  alert(user.name);
  };
  
  $scope.isCollapsed = false;
  nexus.scheduleForm=$scope;
  
   $scope.showEmail = function() {
        return nexus.headerScope.notLogin;
    }
  $scope.schedule = function (){
		
		var ploc=$location.url();
               
//		if(nexus.headerScope.notLogin){
//			nexus.headerScope.turl=function(){
//				//$location.path();
//				$location.url(ploc);
//			};
//			nexus.headerScope.showLogin(ploc);
//			return;
//		}

var startDate = $('#datetimepicker').data('datetimepicker').getLocalDate();
var endDate = $('#datetimepicker').data('datetimepicker').getLocalDate();
if($scope.selectedPeriod.id==undefined) {
	endDate.setMinutes(endDate.getMinutes()+$scope.selectedPeriod[0]);
} else {
	endDate.setMinutes(endDate.getMinutes()+$scope.selectedPeriod.id);
}
var reqData = {"description" : $('#scheduleMessage').val(), "startTime": startDate, "endTime": endDate, 
		"categoryName": $('#selectedCategory option:selected').text(),
            "email":$('#email').text()};

var userListRef = new Firebase("https://adviseme.firebaseio.com/");
var myUserRef = userListRef.child($stateParams.id);


$http.post('counsel/'+ $stateParams.id+'/addCounselRequest', reqData).success(function(data) {
	myUserRef.set({ message: $('#scheduleMessage').val()});
//        if(nexus.headerScope.notLogin){
//          have to show a success message  
//        }else{
//            $location.path('/dashboard/sessions');
//        }
	$location.path('/dashboard/sessions');
});
  };
	
  $scope.sendMessage = function (){
	
			var ploc=$location.url();
			if(nexus.headerScope.notLogin){
				nexus.headerScope.turl=function(){
					//$location.path();
					$location.url(ploc);
				};
				nexus.headerScope.showLogin();
				return;
			}

	 var reqData = {"message" : $('#textmessage').val(), "toUser":{"id":$stateParams.id}};
	 var userListRef = new Firebase("https://adviseme.firebaseio.com/");
	 var myUserRef = userListRef.child($stateParams.id);
	 
	 $http.post('message', reqData).success(function(data) {
		 
		 myUserRef.set({ message: $('#textmessage').val()});
		 $('#textmessage').val('');
		 alert("Message Posting "+data);
		//$location.path('/dashboard');
	});
	
		//}
	};
	
	 $scope.addFavExpert = function (){
		 var reqData = { "expertId":$stateParams.id};
		 $http.get('addFavExpert/'+$stateParams.id).success(function(data) {
			 alert("Expert is added to favorites.");
			//$location.path('/dashboard');
		});
	 };
	 
	$scope.periods = [{id:15, name:"15 min"}, {id:30,name:"30 min"},{id:60 ,name:"1 hr"}];
	$scope.selectedPeriod = $scope.periods[0];
}
userCtrl.$inject = ['$scope','$stateParams','$http', '$location'];//inject required




